#include "widget.h"
#include "ui_widget.h"

#include <QKeyEvent>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

// 按键事件
/*
void Widget::keyPressEvent(QKeyEvent *event){
    // 判断是否是 ctrl 键
    if(event->modifiers() == Qt::ControlModifier){
        // 判断是否是 M 键 ==> 是否是 ctrl M ==> 不是全屏就设置全屏
        if(event->key() == Qt::Key_M && windowState() != Qt::WindowFullScreen){
            setWindowState(Qt::WindowFullScreen);
            return;
        }
        return;
    }

    // 如果按下的是 esc 并且是全屏的状态，就取消全屏
    if(event->key() == Qt::Key_Escape && windowState() == Qt::WindowFullScreen){
        setWindowState(Qt::WindowNoState);
        return;
    }
}
*/

// 控制按钮移动
// 这里的坐标和数学上的坐标是不一样的：x 轴是向右的，y 轴是向下的(数学里面的 y 轴是向上的)
void Widget::keyPressEvent(QKeyEvent *event){
    bool b_upflag= false;
    bool b_downflag = false;
    bool b_leftflag = false;
    bool b_rightflag = false;
    if(event->key() == Qt::Key_Up){
        if(event->isAutoRepeat()){
            auto curpos = ui->pushButton->pos();
            curpos.setY(curpos.y() - 5);
            ui->pushButton->move(curpos);
            return;
        }else{
            b_upflag = true;
        }
    }
    if(event->key() == Qt::Key_Left){
        if(event->isAutoRepeat()){
            auto curpos = ui->pushButton->pos();
            curpos.setX(curpos.x() - 5);
            ui->pushButton->move(curpos);
            return;
        }else{
            b_leftflag = true;
        }
    }
    if(event->key() == Qt::Key_Down){
        if(event->isAutoRepeat()){
            auto curpos = ui->pushButton->pos();
            curpos.setY(curpos.y() + 5);
            ui->pushButton->move(curpos);
            return;
        }else{
            b_downflag = true;
        }
    }
    if(event->key() == Qt::Key_Right){
        if(event->isAutoRepeat()){
            auto curpos = ui->pushButton->pos();
            curpos.setX(curpos.x() + 5);
            ui->pushButton->move(curpos);
            return;
        }else{
            b_rightflag = true;
        }
    }

    auto curpos = ui->pushButton->pos();
    if(b_upflag){
        curpos.setY(curpos.y() - 5);
    }
    if(b_downflag){
        curpos.setY(curpos.y() + 5);
    }
    if(b_leftflag){
        curpos.setX(curpos.x() - 5);
    }
    if(b_rightflag){
        curpos.setX(curpos.x() + 5);
    }
    ui->pushButton->move(curpos);
}
